﻿#include "ssClockWheelThread.h"


namespace StarSeeker
{

ssClockWheelThread::ssClockWheelThread()
{
}

ssClockWheelThread::~ssClockWheelThread()
{
	ExitThread();
}

void ssClockWheelThread::StartHalfHourTimer()
{
	SS_MUTEX_LOCK(m_mutex)
		StartHalfTimer();
	SS_MUTEX_UNLOCK(m_mutex)
}

StarSeeker::u32 ssClockWheelThread::AddNewTimer(u32 msgType, u64 timestamp, u32 interval, ulint p1, ulint p2)
{
	u32 id = 0;
	SS_MUTEX_LOCK(m_mutex)
		id = AddTimer(msgType, timestamp, interval, p1, p2);
	SS_MUTEX_UNLOCK(m_mutex)
	return id;
}

u32 ssClockWheelThread::AddNewTimer(u32 msgType, u32 interval, ulint param1, ulint param2)
{
	u32 id = 0;
	SS_MUTEX_LOCK(m_mutex)
		id = AddTimer(msgType, interval, param1, param2);
	SS_MUTEX_UNLOCK(m_mutex)
	return id;
}

void ssClockWheelThread::DeleteTimer(u32 timerId)
{
	SS_MUTEX_LOCK(m_mutex);
	RemoveTimer(timerId);
	SS_MUTEX_UNLOCK(m_mutex);
}

void ssClockWheelThread::ExitThread()
{
	RequestStop();
}

void ssClockWheelThread::ThreadMemberFunc()
{
	while (!IsRequestStop())
	{
		m_timer.UpdateTime();
		if (m_timer.GetDifference().i64p > 0)
		{
			for (i64 i = 0; i < m_timer.GetDifference().i64p; ++i)
			{
				SS_MUTEX_LOCK(m_mutex);
				AddTick();
				SS_MUTEX_UNLOCK(m_mutex);
			}
		}
		ssThreadBase::Sleep(200);
	}
}

}
